from keras.models import load_model
import numpy as np
from generate_train_data import generate_test_data


model = load_model("../model/tenpai.model")
x_data, y_data, assistant_data = generate_test_data('test')

pad_dim = int(model.input.shape[1])
#输入张量

def pad_sample(x, pad_dim):#垫子样例
    zeros = np.zeros((pad_dim - x.shape[0], 52))
    #action嵌入到一个52维向量中
    #返回来一个给定形状和类型的用0填充的数组；
    return np.concatenate((zeros, x), axis=0).reshape(1, pad_dim, 52)
    #完成多个数组的拼接
    #二维数组变为一维数组

def predict(x, sute):#返回预测概率
    p = model.predict(pad_sample(x, pad_dim))[0]
    #模型预测,输入测试集,输出预测结果
 
    for i in range(len(sute)):
        if sute[i]:
            p[i] = 0
    return p / sum(p)


def number_to_tile(num):        #译码，把number转化为名称
    if num <= 26:
        k = num // 9
        numb = num % 9 + 1
        follow = "m"
        if k == 1:
            follow = "p"
        if k == 2:
            follow = "s"
        return str(numb) + follow
    z_list = "東南西北白発中"
    return z_list[num - 27]


def print_tenpai(y):
    print("Richi player tenpai:",
          [number_to_tile(num) for num in range(34) if y[num] == 1])


def predict_with_assistant(x, y, assist):
    player, sute = assist
    result = dict()
    threshold = 0.01            #输出阈值

    prob = predict(x, sute)     #获取概率列表
    print_tenpai(y)             #输出所有预测
    print("Player " + str(player) + ": ")
    for i in range(len(prob)):
        if prob[i] > threshold:
            result[number_to_tile(i)] = prob[i]
    print(result)


def predict_by_order(i):#按顺序预测
    predict_with_assistant(x_data[i], y_data[i], assistant_data[i])


if __name__ == "__main__":
    cnt = x_data.shape[0]
    for i in range(cnt):
        predict_by_order(i)

